package com.jic.data.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.data.constant.consist.ResponseCode;
import com.jic.data.exception.AppRuntimeException;
import com.jic.data.mapper.MemberActiveTimeIntervalStsMapper;
import com.jic.data.model.entity.MemberActiveTimeIntervalSts;
import com.jic.data.model.entity.MemberIsconsumeSts;
import com.jic.data.service.MemberActiveTimeIntervalStsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class MemberActiveTimeIntervalStsServiceImpl implements MemberActiveTimeIntervalStsService {

    @Resource
    private MemberActiveTimeIntervalStsMapper mapper;


    /**
     * 新增记录
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存成功
     */
    @Override
    public int save(MemberActiveTimeIntervalSts request) {
        int flag = mapper.insert(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
        }
    }


    /**
     * 保存或修改记录信息
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存或修改成功
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:09
     */
    @Override
    public int saveOrUpdate(MemberActiveTimeIntervalSts request) {
        log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
        try {
            if (request == null) {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "入参为空");
            }
            if (request.getId() != null && request.getId() != 0) {
                log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.updateByPrimaryKey(request);
                if (flag == 1) {
                    return 1;
                }
            } else {
                log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.insertSelective(request);
                if (flag == 1) {
                    return 1;
                } else {
                    throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改失败");
                }
            }
        } catch (Exception e) {
            log.error("用户保存或修改失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "用户保存或修改失败");
        }
        return -1;
    }

    /**
     * 删除记录
     *
     * @param request 需要删除,含有主键的入参实体类
     * @return int 是否删除成功
     */
    @Override
    public int deleteByPrimaryKey(MemberActiveTimeIntervalSts request) {
        int flag = mapper.deleteByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
        }
    }


    /**
     * 逻辑删除记录
     *
     * @param request 含有主键的逻辑删除实体类
     * @return int 是否逻辑删除成功
     */
    @Override
    public int deleteByPrimaryKeyLogically(MemberActiveTimeIntervalSts request) {
        request.setDeleteFlag(1);
        int flag = mapper.updateByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
        }
    }


    /**
     * 修改
     *
     * @param request
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(MemberActiveTimeIntervalSts request) {
        try {
            log.info("修改记录");
            MemberActiveTimeIntervalSts param = new MemberActiveTimeIntervalSts();
            BeanUtils.copyProperties(request, param);
            int flag = mapper.updateByPrimaryKeySelective(param);
            if (flag == 1) {
                return 1;
            } else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
            }
        } catch (Exception e) {
            log.error("修改记录失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
        }

    }

    /**
     * 记录详情
     *
     * @param request 查询的入参实体类
     * @return 数据库查询到的实体类
     */
    @Override
    public MemberActiveTimeIntervalSts getByPrimaryKey(MemberActiveTimeIntervalSts request) {
        return mapper.selectByPrimaryKey(request);
    }


    /**
     * 分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public PageInfo<MemberActiveTimeIntervalSts> listPageBySelective(MemberActiveTimeIntervalSts request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<MemberActiveTimeIntervalSts> list = mapper.listPageBySelective(request);
        return new PageInfo(list);
    }

    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public List<MemberActiveTimeIntervalSts> listAllRecord(MemberActiveTimeIntervalSts request) {
        //分页处理
        request.setDeleteFlag(0);
        return mapper.selectBySelective(request);
    }


    /**
     * 会员【活跃时间段】统计-饼状图
     * @param request 查询入参实体类
     * @return 实体类列表
     * @author tzc
     * @date 2020-5-21 11:46:19
     */
    @Override
    public Map<String,Integer> getDistributeOfActiveTime(MemberActiveTimeIntervalSts request) {
        request.setDeleteFlag(0);
        List<MemberActiveTimeIntervalSts> list = mapper.selectBySelective(request);
        Integer less30       =0;
        Integer total30To60  =0;
        Integer total60To90  =0;
        Integer total90To120 =0;
        Integer total120To150=0;
        Integer total150To180=0;
        Integer total180To210=0;
        Integer total210To240=0;
        Integer total240To270=0;
        Integer total270To300=0;
        Integer total300To330=0;
        Integer total330To360=0;
        Integer total360To365=0;
        Integer more365=0;
        if (list != null  && list.size()!=1){
            throw new AppRuntimeException("无法获取唯一的记录");
        }

        MemberActiveTimeIntervalSts memberActiveTimeIntervalSts = list.get(0);
        String periodStsData = memberActiveTimeIntervalSts.getPeriodStsData();
        JSONArray jsonArray = JSONArray.parseArray(periodStsData);
        for (Object json: jsonArray) {
            JSONObject jsonObject = (JSONObject) json;
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("0-30")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        less30 = less30 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("30-60")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total30To60 = total30To60 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("60-90")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total60To90 = total60To90 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("90-120")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total90To120 = total90To120 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("120-150")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total120To150 = total120To150 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("150-180")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total150To180 = total150To180 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("180-210")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total180To210 = total180To210 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("210-240")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total210To240 = total210To240 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("240-270")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total240To270 = total240To270 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("270-300")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total270To300 = total270To300 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("300-330")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total300To330 = total300To330 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("330-360")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total330To360 = total330To360 + Integer.valueOf(memberNumber+"");
                    }
                }
            }

            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("360-365")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        total360To365 = total360To365 + Integer.valueOf(memberNumber+"");
                    }
                }
            }
            if (jsonObject.containsKey("recencyPeriodName") && jsonObject.get("recencyPeriodName").equals("365+")){
                if (jsonObject.containsKey("memberNumber") && null != jsonObject.get("memberNumber")){
                    Object memberNumber = jsonObject.get("memberNumber");
                    if (StringUtils.isNumber(String.valueOf(memberNumber))){
                        more365 = more365 + Integer.valueOf(memberNumber+"");
                    }
                }
            }


        }

        Map<String,Integer> map = new HashMap<>();
        map.put("less30",less30);
        map.put("total30To60",total30To60);
        map.put("total60To90",total60To90);
        map.put("total90To120",total90To120);
        map.put("total120To150",total120To150);
        map.put("total150To180",total150To180);
        map.put("total180To210",total180To210);
        map.put("total210To240",total210To240);
        map.put("total240To270",total240To270);
        map.put("total270To300",total270To300);
        map.put("total300To330",total300To330);
        map.put("total330To360",total330To360);
        map.put("total360To365",total360To365);
        map.put("more365",more365);

        return map;
    }


}
